// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class OrderDetailsMapper
    {

        /// <summary />
        static OrderDetailsMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<OrderDetails> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.OrderDetailsSelectAll;

            return Load(commands, dataMappers);
        }

        private OrderDetails.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            OrderDetails.DataTransfer data=new OrderDetails.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static OrderDetails.DataTransfer FillData(OrderDetails.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("OrderID");
            if (!record.IsDBNull(ord))
                data.OrderID=record.GetInt32(ord);

            ord=record.GetOrdinal("ProductID");
            if (!record.IsDBNull(ord))
                data.ProductID=record.GetInt32(ord);

            ord=record.GetOrdinal("UnitPrice");
            if (!record.IsDBNull(ord))
                data.UnitPrice=record.GetDecimal(ord);

            ord=record.GetOrdinal("Quantity");
            if (!record.IsDBNull(ord))
                data.Quantity=record.GetInt16(ord);

            ord=record.GetOrdinal("Discount");
            if (!record.IsDBNull(ord))
                data.Discount=record.GetFloat(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderDetailsSelect,
                parameterBuilder.GetMarker("OrderID", 1),
                parameterBuilder.GetMarker("ProductID", 2)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 2);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderDetailsDelete,
                parameterBuilder.GetMarker("OrderID", 1),
                parameterBuilder.GetMarker("ProductID", 2)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.OrderID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 2);
            p.DbType=DbType.Int32;
            p.Value=data.ProductID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderDetailsInsert,
                parameterBuilder.GetMarker("OrderID", 1),
                parameterBuilder.GetMarker("ProductID", 2),
                parameterBuilder.GetMarker("UnitPrice", 3),
                parameterBuilder.GetMarker("Quantity", 4),
                parameterBuilder.GetMarker("Discount", 5)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.OrderID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 2);
            p.DbType=DbType.Int32;
            p.Value=data.ProductID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitPrice", 3);
            p.DbType=DbType.Decimal;
            p.Value=data.UnitPrice;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Quantity", 4);
            p.DbType=DbType.Int16;
            p.Value=data.Quantity;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Discount", 5);
            p.DbType=DbType.Single;
            p.Value=data.Discount;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderDetailsUpdate,
                parameterBuilder.GetMarker("UnitPrice", 1),
                parameterBuilder.GetMarker("Quantity", 2),
                parameterBuilder.GetMarker("Discount", 3),
                parameterBuilder.GetMarker("OrderID", 4),
                parameterBuilder.GetMarker("ProductID", 5)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitPrice", 1);
            p.DbType=DbType.Decimal;
            p.Value=data.UnitPrice;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Quantity", 2);
            p.DbType=DbType.Int16;
            p.Value=data.Quantity;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Discount", 3);
            p.DbType=DbType.Single;
            p.Value=data.Discount;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 4);
            p.DbType=DbType.Int32;
            p.Value=data.OrderID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 5);
            p.DbType=DbType.Int32;
            p.Value=data.ProductID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;

    }

}
